const Router = require('koa-router');
const User = require('../models/users');
const router = new Router();

// 用户注册
router.post('/register', async (ctx) => {
    const { login_name, password } = ctx.request.body;
    User.register(login_name, password);
    ctx.body = { success: true };
});

// 用户登录
router.post('/login', async (ctx) => {
    const { login_name, password } = ctx.request.body;
    const user = User.findByName(login_name);
    if (user && user.password === password) {
        ctx.body = { success: true, userId: user.id };
    } else {
        ctx.body = { success: false, message: 'Invalid credentials' };
    }
});

module.exports = router;